package com.jeff.tianti.controller;

import com.jeff.tianti.common.SMS.SmsSendUtils;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/verificate")
@CrossOrigin(maxAge = 3600)
public class VerificateCode {
    @Autowired
    SmsSendUtils smsSendUtils;
    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping(value = "/code", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AjaxResult getVeriCode(@RequestBody Map map) {
        String mobile = map.get("mobile").toString();
        String verificationCode = smsSendUtils.getVerificationCode(mobile);
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        redisUtil.set(mobile, verificationCode, 60);
        return AjaxResult.setSuccessData(verificationCode);
    }

}
